﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MvcMusicStore.Controllers
{
    public class SearchController : Controller
    {
        // GET: /Search/
        private MusicShopDBEntities model = new MusicShopDBEntities();

        public ActionResult Index(string tosearch)
        {
            
                ViewBag.tosearch = tosearch;
                return View();
           
        }
    
        [ChildActionOnly]
        public ActionResult FindSongs(string tosearch)
        {
            
                if (string.IsNullOrEmpty(tosearch))
                {
                    ViewBag.result = false;
                    return PartialView();
                }
                else
                {
                    var songs = GetSearchSongs(tosearch);
                    ViewBag.result = true;
                    return PartialView(songs);
                }
            
        }
        [ChildActionOnly]
        public ActionResult FindAlboms(string tosearch)
        {
           
                if (string.IsNullOrEmpty(tosearch))
                {
                    ViewBag.result = false;
                    return PartialView();
                }
                else
                {
                    var alboms = GetSearchAlbums(tosearch);
                    ViewBag.result = true;
                    return PartialView(alboms);
                }
            
        }

        private List<Album> GetSearchAlbums(string tosearch)
        {
            
                var search = Server.HtmlEncode(tosearch);
                return model.Album.Include("Author").Include("Song")
                       .Where(t => t.titleAlbum.Contains(search) || t.Author.nameAuthor.Contains(search))
                       .Distinct()
                       .ToList();
            
        }

        private List<Song> GetSearchSongs(string tosearch)
        {
           
                var search = Server.HtmlEncode(tosearch);
                return model.Song.Include("Author").Include("Album")
                       .Where(t => t.titleSong.Contains(search) || t.Author.nameAuthor.Contains(search))
                       .Distinct()
                       .ToList();
            
        }

        protected override void Dispose(bool disposing)
        {
            model.Dispose();
            base.Dispose(disposing);
        }
    }
}
//public ActionResult Index(string tosearch)
//{

//    if (string.IsNullOrEmpty(tosearch))
//    {
//        ViewBag.result = false;
//        return View();
//    }
//    else
//    {
//        var search = Server.HtmlEncode(tosearch);
//        var song = model.Song.Include("Album").Include("Author")
//           .Where(t => t.titleSong.Contains(search) || t.Album.titleAlbum.Contains(search) || t.Author.nameAuthor.Contains(search))
//           .Distinct();
//        ViewBag.result = true;
//        return View(song);
//    }
//}
